import { _decorator, Component, Node } from 'cc';
import { UserModel } from './models/user';
import { Login } from './Login';
const { ccclass, property } = _decorator;

@ccclass('PrivacyPage')
export class PrivacyPage extends Component {

    @property(Node)
    private userBtn: Node = null;
    @property(Node)
    private privacyBtn: Node = null;

    @property(Node)
    private agreeBtn: Node = null;
    @property(Node)
    private noBtn: Node = null;

    start() {
        this.userBtn.on(Node.EventType.TOUCH_START, this.onUserBtn, this);
        this.privacyBtn.on(Node.EventType.TOUCH_START, this.onPrivacyBtn, this);
        this.agreeBtn.on(Node.EventType.TOUCH_START, this.onAgreeBtn, this);
        this.noBtn.on(Node.EventType.TOUCH_START, this.onNoBtn, this);
    }

    private onAgreeBtn(){
        this.node.active = false;
    }

    private onNoBtn(){
        try{
            // 退出游戏
            UserModel.getInstance().clearUserInfo();
            // 退出app
            this.exitApplication();
        }catch(e){
            console.error("退出游戏失败:", e);
        }
    }

    private onPrivacyBtn(){
       this.node.parent.parent.getComponent(Login).openPrivate();
    }

    private onUserBtn(){
        this.node.parent.parent.getComponent(Login).openAgreement();
    }
    
    /**
     * 退出应用程序
     * 根据不同平台调用相应的退出方法
     */
    exitApplication() {
        // 微信小游戏
        if (sys.platform === sys.Platform.WECHAT_GAME) {
            if (typeof wx !== 'undefined') {
                wx.exitMiniProgram({
                    success: () => {
                        console.log("成功退出微信小游戏");
                    },
                    fail: (err) => {
                        console.error("退出微信小游戏失败:", err);
                    }
                });
            }
        } 
        // 原生平台 - Android
        else if (sys.platform === sys.Platform.ANDROID) {
            if (typeof jsb !== 'undefined') {
                jsb.reflection.callStaticMethod(
                    "com/cocos/game/AppActivity",  // 替换为您的Java类路径
                    "exitApplication",
                    "()V"
                );
            } else {
                // 通过BridgeManager调用
                const BridgeManager = require('./BridgeManager').BridgeManager;
                BridgeManager.getInstance().callNative("exitApp", "");
            }
        } 
        // 原生平台 - iOS
        else if (sys.platform === sys.Platform.IOS) {
            if (typeof jsb !== 'undefined') {
                jsb.reflection.callStaticMethod(
                    "AppController",
                    "exitApplication"
                );
            } else {
                // 通过BridgeManager调用
                const BridgeManager = require('./BridgeManager').BridgeManager;
                BridgeManager.getInstance().callNative("exitApp", "");
            }
        }
        // 浏览器环境 - 开发调试时
        else if (sys.isBrowser) {
            console.log("浏览器环境无法直接退出应用，关闭当前页面");
            window.close(); // 尝试关闭页面
        }
    }
}


